package com.china08.yxyapi.api.temp;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

@CrossOrigin
@RestController
public class ResourceFileDuraditonUpdController {

    @Autowired
    private ElasticsearchTemplate et;

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @Value("${weike.storage.upload.video}")
    private String uploadBasePath;

    /**
     * 
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/temp/resource/duration", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String process(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("资源微课时间为0的问题...................");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        TermQueryBuilder builder = new TermQueryBuilder("duration", 0);
        TermQueryBuilder builder1 = new TermQueryBuilder("typeId", "yx14");

        boolQueryBuilder.must(builder);
        boolQueryBuilder.must(builder1);
        boolQueryBuilder.minimumNumberShouldMatch(2);
        SearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();

        long l = et.count(query, ElasticYxyResResource.class);
        
        Pageable pageable = new PageRequest(0, Integer.parseInt(l + ""));
        
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withPageable(pageable).build();
        
        List<ElasticYxyResResource> list = et.queryForList(searchQuery, ElasticYxyResResource.class);


        System.out.println("资源微课时间为0的一共：" + list.size());

        File source = null;
        for (ElasticYxyResResource elasticYxyResResource : list) {
            source = new File(uploadBasePath + elasticYxyResResource.getLocation());
            Integer duration = 0;
            if (source.exists()) {
                try {
                    Encoder encoder = new Encoder();
                    MultimediaInfo multimediaInfo = encoder.getInfo(source);
                    long ls = multimediaInfo.getDuration() / 1000;
                    duration = Integer.valueOf(ls + "");
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
            }
            if (duration != 0) {
                YxyResResource resource = yxyResResourceRepository.findById(elasticYxyResResource.getId());
                resource.setDuration(duration);
                yxyResResourceRepository.save(resource);
                EsResourceUpdateUtil.updateEsResoure(elasticYxyResResource.getId());
            }
        }
        return "资源微课时间为0的问题处理结束";
    }

}
